package com.ciquan.server.controller;

import com.ciquan.server.bean.ShareRecord;
import com.ciquan.server.service.ShareRecordService;
import com.ciquan.server.util.web.Result;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.ServletRequestUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * Created by card on 2015/8/30.
 */
@Controller
@RequestMapping("/api/share/")
public class ServiceShareRecordController extends AbstractController {

    @Resource
    private ShareRecordService shareRecordService;

    @RequestMapping(value = "/share", produces = "text/plain;charset=UTF-8", method = {RequestMethod.POST})
    @ResponseBody
    public String share(HttpServletRequest request) {
        Result result = new Result();
        result.setSuccess(false);
        if(!isLogin(request)) {
            result.setStatusmsg("用户未登录");
        }

        else {
            try {
                int userId = ServletRequestUtils.getIntParameter(request, "userId", 0);
                int worksId = ServletRequestUtils.getIntParameter(request, "worksId", 0);
                int platform = ServletRequestUtils.getIntParameter(request, "platform", 0);
                ShareRecord shareRecord = new ShareRecord();
                shareRecord.setUserId(userId);
                shareRecord.setWorksId(worksId);
                shareRecord.setPlatform(platform);
                shareRecordService.insert(shareRecord);
                result.setSuccess(true);
                result.setStatusmsg("分享成功");
            } catch (Exception e) {
                logger.error(e);
                result.setStatusmsg("您已经分享过了");
            }
        }
        return result.toString();
    }
}
